package com.afkt.project.ui.slice;


import com.afkt.project.model.bean.MyListBean;
import com.afkt.project.ui.adapter.MyListAdapter;
import com.dev.receiver.WifiEventSubscriber;
import com.dev.utils.WifiUtils;
import com.dev.utils.app.OpenNetUtils;
import com.ohos.afkt.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;
import ohos.agp.window.dialog.ToastDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * wifi相关（热点）的slice
 */
public class WifiSlice extends AbilitySlice {

    private ListContainer listContainer;
    private MyListAdapter mAdapter;

    WifiEventSubscriber subscriber;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_layout_wifi);
        //注册wifi监听
        subscriber = WifiUtils.initMatch(WifiSlice.this);
        initList();
    }

    private void initList() {
        List<MyListBean> list = initData();
        listContainer = (ListContainer) findComponentById(ResourceTable.Id_wifi_list);
        mAdapter = new MyListAdapter(list, this);
        listContainer.setItemProvider(mAdapter);
        listContainer.setItemClickedListener((listContainer, component, position, l) -> {
            switch (position) {
                // 打开wifi
                case 0:
                    new ToastDialog(getContext()).setText("暂时只能跳转系统设置")
                            .show();
                    startAbility(OpenNetUtils.openNet());
                    break;
                // 关闭wifi
                case 1:
                    new ToastDialog(getContext()).setText("暂时只能跳转系统设置")
                            .show();
                    startAbility(OpenNetUtils.openNet());
                    break;
                // 打开wifi热点
                case 2:
                    new ToastDialog(getContext()).setText("暂时只能跳转系统设置")
                            .show();
                    startAbility(OpenNetUtils.openNet());
                    break;
                // 关闭wifi热点
                case 3:
                    new ToastDialog(getContext()).setText("暂时只能跳转系统设置")
                            .show();
                    startAbility(OpenNetUtils.openNet());
                    break;
                // 注册wifi监听
                case 4:
                    new ToastDialog(getContext()).setText("Wifi监听成功,请查看Logcat")
                            .show();
                    WifiUtils.subscribeWifi(subscriber);
                    break;
                // 注销wifi监听
                case 5:
                    new ToastDialog(getContext()).setText("注销Wifi监听成功").show();
                    WifiUtils.unSubscribeWifi(subscriber);
                    break;
                default:
                    break;
            }
        });
    }

    private List<MyListBean> initData() {
        List<MyListBean> lists = new ArrayList<>();
        lists.add(new MyListBean("打开wifi"));
        lists.add(new MyListBean("关闭wifi"));
        lists.add(new MyListBean("打开wifi热点"));
        lists.add(new MyListBean("关闭wifi热点"));
        lists.add(new MyListBean("注册wifi监听"));
        lists.add(new MyListBean("注销wifi监听"));
        return lists;
    }
}
